$(document).ready(function(){
    if(window.location.pathname=='/newsDetail.html'||window.location.pathname=='/policyDetail.html'){
        var title=localStorage.getItem("title");
        var time=localStorage.getItem("time");
        var content=localStorage.getItem("content");
        $('#title').text(title);
        $('#time').text(time);
        $('#content').text(content);
    }else{
        var data=[];
        var aList=[];
        var pageNews=$(".page-news");
        var newsList=$(".news-list");
        var total=0;
        var clickNews=function(){
            for(var i=0;i<aList.length;i++){
                aList[i].onclick=function (e) {
                    var index=e.target.id;
                    var href=window.location.hostname;
                    console.log(href)
                    window.location.href="http://"+href+":8888/policyDetail.html";
                }
            }
        }
        $.ajax({
            type: "POST",
            url: 'http://localhost:8888/bank/getProductDeal',
            success: function (result) {
                if(result.meta.success){
                    data=result.data["Deal List"];
                    var data1=data.slice(0,10).map(function (item,index) {
                        return '<li class="news_item"><span class="new_link"><a href="javascript:void(0)" id="'+index+'">'+item.enterpriseName+'与'+item.bankName+'对接成功</a></span><span class="new_date">'+item.dealTime.slice(0,10)+'</span></li>';
                    });
                    newsList.remove();
                    pageNews.append('<ul class="news-list"></ul>');
                    var newsUl=$('.news-list');
                    newsUl.append(data1);
                    total=Math.ceil(data.length/10);
                    $('#total-page').text('共'+total+'页');
                    aList=$('.new_link');
                    clickNews()
                }else{
                    alert(result.meta.message);
                }
            },
            error:function(){
                alert("网络错误");
            }
        });

        $(".page-next").click(function () {
            var pageNum=parseInt($("#page-num").text());
            if(pageNum<total){
                $("#page-num").html(pageNum+1);
                $(".news-list").remove();
                var data1=data.slice((pageNum)*10,(pageNum+1)*10).map(function (item,index) {
                    return '<li class="news_item"><span class="new_link"><a href="javascript:void(0)" id="'+index+'">'+item.enterpriseName+'与'+item.bankName+'对接成功</a></span><span class="new_date">'+item.dealTime.slice(0,10)+'</span></li>';
                });
                pageNews.append('<ul class="news-list"></ul>');
                var newsUl=$('.news-list');
                newsUl.append(data1);
                aList=$('.new_link');
                clickNews()
            }
        })
        $(".page-prev").click(function () {
            var pageNum=parseInt($("#page-num").text());
            if(pageNum>1){
                $("#page-num").html(pageNum-1);
                $(".news-list").remove();
                var data1=data.slice((pageNum-2)*10,(pageNum-1)*10).map(function (item,index) {
                    return '<li class="news_item"><span class="new_link"><a href="javascript:void(0)" id="'+index+'">'+item.enterpriseName+'与'+item.bankName+'对接成功</a></span><span class="new_date">'+item.dealTime.slice(0,10)+'</span></li>';
                });
                pageNews.append('<ul class="news-list"></ul>');
                var newsUl=$('.news-list');
                newsUl.append(data1);
                aList=$('.new_link');
                clickNews()
            }
        })
    }

});